package com.spc.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.spc.dao.CheckItemDao;
import com.spc.entity.PageResult;
import com.spc.entity.QueryPageBean;
import com.spc.pojo.CheckItem;
import com.spc.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查项服务
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    //注入dao对象
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //取出分页条件
        Integer currentPage = queryPageBean.getCurrentPage();//当前页码
        Integer pageSize = queryPageBean.getPageSize();//每页记录数
        String queryString = queryPageBean.getQueryString();//查询条件
        //调用dao完成分页查询，使用mybatis分页助手插件
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page = checkItemDao.findByCondition(queryString);
        //使用分页助手中的方法获取总记录数和分页数据
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        PageResult pageResult = new PageResult(total, rows);

        return pageResult;
    }

    @Override
    public void deleteOne(Integer id) {
        //判断检查项是否关联检查组
        long count = checkItemDao.findCountById(id);
        if (count>0){
            throw new RuntimeException();
        }
        checkItemDao.deleteOne(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem = checkItemDao.findById(id);
        return checkItem;
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
